<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;
use App\Post;
use App\Comment;
use App\Zan;
class PostController extends Controller
{
    /*
        点赞功能
    */
    public function zan(Post $post){
            $arr=[
                    'post_id'=>$post->id,
                    'user_id'=>\Auth::id(),

            ];
            Zan::create($arr);
            return back();
    }
    /*
        取消点赞功能
    */
    public function uzan(){
            $user_id=\Auth::id();
            Zan::where(compact($user_id))->delete();
             return back();
    }
      /*
        提交评论
    */

    public function comment(Request $request){
           
            //validate
            $this->validate($request,[
                'comtents'=>'required|min:3',
            ]);
            //logic
            $com=[

                'comtents'=>request('comtents'),
                'post_id'=>request('post_id'),
                'user_id'=>\Auth::id(),

                ];
         
            Comment::create($com);
            return back();
    }
    /*
        发帖内容框显示图片
    */
    public function pan(Request $request)
    {
    if ($request->hasFile('file')) {
        $picture = $request->file('file');
        if (!$picture->isValid()) {
            abort(400, '无效的上传文件');
        }
        // 文件扩展名
        $extension = $picture->getClientOriginalExtension();
        // 文件名
        $fileName = $picture->getClientOriginalName();
        // 生成新的统一格式的文件名
        $newFileName = md5($fileName . time() . mt_rand(1, 10000)) . '.' . $extension;
        // 图片保存路径
        $savePath = 'images/' . $newFileName;
        // Web 访问路径
        $webPath = '/storage/' . $savePath;
        // 将文件保存到本地 storage/app/public/images 目录下，先判断同名文件是否已经存在，如果存在直接返回
        if (Storage::disk('public')->has($savePath)) {
            return response()->json(['errno'=>0,'data'=>[$webPath,]]);
        }
        // 否则执行保存操作，保存成功将访问路径返回给调用方
        if ($picture->storePubliclyAs('images', $newFileName, ['disk' => 'public'])) {
            return response()->json(['errno'=>0,'data'=>[$webPath,]]);
        }
        abort(500, '文件上传失败');
    } else {
        abort(400, '请选择要上传的文件');
    }
}
    /**
     * 
     *
     *  显示post主页面内容
     */
    public function index()
    {
                  
         $posts=Post::orderBy('created_at','desc')->withCount(['zan','comments'])->paginate(4);
        return  view('/post/index',compact('posts'));
    }

    /**
   
     显示写文章模板
 
     */
    public function create()
    {
        //

        return view('/post/create');
    }

    /**
        提交写文章的内容
     */
    public function store(Request $request)
    {
        //
           
            $this->validate($request,[
                    'title'=>'required|string|min:2',
                    'content'=>'required|string|min:12',

            ]);
            $user_id=\Auth::id();
            $arr=array_merge(request(['title','content']),compact('user_id'),['status'=>0]);
          
        Post::create($arr);
         return  redirect('/posts');

    }

    /**
        个人文章页面
     */
    public function show(Post $post)
    {
        //
        
        return view('post/show',compact('post'));
    }

    /**
            个人文字编辑
     */
    public function edit(Post $post)
    {
        //

        return view('/post/edit',compact('post'));
    }

    /**
       更新用户文章
            
     */
    public function update(Post $post)
    {
        $this->validate(request(),[

            'title'=>'required',
            'content'=>'required'
        ]);
        $post->title=request('title');
         $post->content=request('content');
         $post->save();
         return  redirect('/posts');
    }

    
}
